package com.mall507.fivepeaks.activity.main;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.comn.model.ConvenientLifeErShouMaiMaiInfo;
import com.mall507.comn.model.ConvenientLifeZhuanRangInfo;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.Banner.BGABanner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

@Route(path = "/main/MainErShouInfoUI")
public class MainErShouInfoUI extends BaseUI {
    @Autowired(name = "id")
    String nid;
    @ViewInject(R.id.head_banner)
    BGABanner head_banner;
    @ViewInject(R.id.time)
    TextView time;
    @ViewInject(R.id.contacts)
    TextView contacts;
    @ViewInject(R.id.phone)
    TextView phone;
    @ViewInject(R.id.describe)
    TextView describe;
    @ViewInject(R.id.village)
    TextView village;
    @ViewInject(R.id.titles)
    TextView titles;
    @ViewInject(R.id.type)
    TextView type;

    @Override
    protected void onCreateBase() {
        ARouter.getInstance().inject(this);
        setContentView(R.layout.ui_main_ershou_info);
        getTitleView().setContent("二手详情");
        loadNetWorkData();
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    private void loadNetWorkData() {
        showLoding();
        BaseHttp.getInstance().query("json/ConvenientLifeErShouMaiMaiInfo.json", getParams(), new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                ConvenientLifeErShouMaiMaiInfo convenientLifeErShouMaiMaiInfo = new Gson().fromJson(response.toString(), ConvenientLifeErShouMaiMaiInfo.class);
                handleData(convenientLifeErShouMaiMaiInfo);
            }
        });
    }

    private void handleData(ConvenientLifeErShouMaiMaiInfo convenientLifeErShouMaiMaiInfo) {
        if (convenientLifeErShouMaiMaiInfo.getCode().equals("1")) {
            dismissLoding();
            ConvenientLifeErShouMaiMaiInfo.DetailsBean details = convenientLifeErShouMaiMaiInfo.getDetails();
            List<ConvenientLifeErShouMaiMaiInfo.DetailsBean.ImgListBean> img_list = details.getImg_list();
            if (img_list != null) {
                head_banner.setAdapter(new BGABanner.Adapter<View, ConvenientLifeErShouMaiMaiInfo.DetailsBean.ImgListBean>() {
                    @Override
                    public void fillBannerItem(BGABanner banner, View itemView, ConvenientLifeErShouMaiMaiInfo.DetailsBean.ImgListBean model, int position) {
                        SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.sdv_item_fresco_content);
                        simpleDraweeView.setImageURI(model.getImgUrl());
                    }
                });
                head_banner.setData(R.layout.homerecycle_top_banner_content, details.getImg_list(), null);
            }
            time.setText("发布：" + details.getTime());
            titles.setText(details.getTitle());
            type.setText("二手类型：" + details.getType());
            village.setText("所在地区：" + details.getVillage());
            describe.setText(details.getDescribe());
            contacts.setText("联系人：" + details.getContacts());
            phone.setText("联系电话：" + details.getPhone());
        }
    }

    private JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("id", nid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }
}
